package com.earnings.okhttputils.utils.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

/**
 * Created by Administrator on 2017/5/8 0008.
 */

public class YViewHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> sparseArray;
    public View itemView;

    public YViewHolder(View itemView) {
        super(itemView);
        AutoUtils.auto(itemView);
        this.itemView = itemView;
        this.sparseArray = new SparseArray<>();
    }

    public YViewHolder setText(int viewId, String text) {
        TextView textView = getView(viewId);
        textView.setText(text);
        return this;
    }

    public YViewHolder setImageResource(int viewId, int resource) {
        ImageView imageView = getView(viewId);
        imageView.setImageResource(resource);
        return this;
    }

    public YViewHolder setTextColor(int viewId, int colorId) {
        TextView textView = getView(viewId);
        textView.setTextColor(itemView.getResources().getColor(colorId));
        return this;
    }

    public <T extends View> T getView(int viewId) {
        View view = sparseArray.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            sparseArray.put(viewId, view);
        }
        return (T) view;
    }

}
